﻿using UnityEngine;
using System.Collections;


public class Ball : Entity
{
    public float scale
    {
        get
        {
            return this._scale;
        }
    }
    public float radis
    {
        get
        {
            return this._radis;
        }
    }
    public bool alive
    {
        get
        {
            return this._alive;
        }
    }
    private bool _alive = true;
    private int _count_for_life = 0;
    private float _scale = 0.0f;
    private float _radis = 0.0f;
    private int _kill_count = 0;
    public float x_delta = 0.0f; //单元圆x比例
    public float y_delta = 0.0f;//单位圆y比例
    public string name = "";//玩家名字
    public float alive_time = 0.0f;
    public override void Update()
    {
        base.Update();
    }

    public override void UpdateMS()
    {
        if (!this.IsValid()) return;
        base.UpdateMS();
        if (!_alive)
        {
            _count_for_life++;
            float t = (Config.ALIVE_COUNT - _count_for_life) * 25.0f / 1000.0f;//  / Config.ALIVE_COUNT;//25 if peer frame delay
            this.alive_time = t;
            if (_count_for_life > Config.ALIVE_COUNT)
            {
                this.SetAlive();
                _count_for_life = 0;
            }
            return;
        }
        //eat food
        ArrayList foods = FoodsMgr.ins.GetEatFoods(this);

        foreach (Food food in foods)
        {
            this.value += food.value;

        }


        ArrayList balls = BallsMgr.ins.GetEatBalls(this);

        foreach (Ball ball in balls)
        {
            this.EatBall(ball);
            this._kill_count++;

        }

        this._scale = Mathf.Sqrt((float)this.value / Mathf.PI) + 3.0f;
        this._radis = 1.76f * this._scale;

        if (dir == -1)
        {
            return;
        }

        //   float[] pos_factor_x = { 1.0f, 0.70710678118655f, 0.0f, -0.70710678118655f, -1.0f, -0.70710678118655f, 0.0f, 0.70710678118655f };
        //     float[] pos_factor_y = { 0.0f, 0.70710678118655f, 1.0f, 0.70710678118655f, 0.0f, -0.70710678118655f, -1.0f, -0.70710678118655f };


        ///  float factor = 0.1f;
        float speed = 3.0f;
        //   speed -= this._scale * factor;
        //  if (speed < 1.0f) factor = 0.01f;

        //    Debug.Log(Mathf.Pow(0.99f,radis) +  "   " + radis);
        speed = Mathf.Pow(0.99f, radis) * 3.0f;

        int d = this.dir;
        if (d <= 0) d = 0;
        float dd = d * DATA.ONE_DEGREE;//一度的弧度

        y_delta = Mathf.Sin(dd);
        x_delta = Mathf.Cos(dd);



        if (x < 0) x = 0;
        if (x > Config.MAX_MAP_WIDTH) x = Config.MAX_MAP_WIDTH;

        if (y < 0) y = 0;
        if (y > Config.MAX_MAP_HEIGHT) y = Config.MAX_MAP_HEIGHT;

        this.y = this.y + speed * y_delta;
        this.x = this.x + speed * x_delta;

    }


    public void EatBall(Ball who)
    {
        this.value += who.value;
        who.SetDeath(this);
    }
    public override void OnExit()
    {
        BallsMgr.ins.Remove(this);
        Debug.Log("Ball   exit");
        base.OnExit();
    }

    private void SetDeath(Ball who)
    {//已死亡,
       ArrayList self = BallsMgr.ins.GetBallsByNo(this.no);

       if (self.Count > 1)
       {
           this.LazyDispose();
           EventDispatcher.ins.PostEvent(Event.ID_ONE_SPLIT_DEATH,this);
       }
       else if (this == BallsMgr.ins.GetSelfBalls() && self.Count <= 1)
       {// 自己被吃  只剩下一个  ，显示死亡信息

           EventDispatcher.ins.PostEvent("death", string.Format(DATA.UI_DEATH_KILL, this.value, _kill_count, who.no));
       }
       else
       {

       }




        /* ArrayList self = BallsMgr.ins.GetBallsByNo(BallsMgr.ins.me_no);

         if (self.Count > 1)
         {
             AutoReleasePool.ins.AddObject(this);

             EventDispatcher.ins.PostEvent(Event.ID_ONE_SPLIT_DEATH,this);
         }
         else if (this == BallsMgr.ins.GetSelfBalls() && self.Count <= 1)
         {// self been eat

             EventDispatcher.ins.PostEvent("death", string.Format(DATA.UI_DEATH_KILL, this.value, _kill_count, who.no));
         }
         else
         {

         }*/

        this._alive = false;
        this.value = 1;
    }
    private void SetAlive()
    {
        this._alive = true;
        _kill_count = 0;
        EventDispatcher.ins.PostEvent("alive");

    }

    public static bool operator >(Ball a, Ball b)
    {
        return a.value > b.value;
    }
    public static bool operator <(Ball a, Ball b)
    {
        return a.value < b.value;
    }
}










